<template>
  <view class="content">
    <u--form
      labelPosition="left"
      labelWidth="65%"
      :model="model"
      :rules="rules"
      ref="uForm"
    >
      <u-form-item
        class="item"
        @click="handleImg"
        :flexAble="false"
        className="img-formItem"
      >
        <view class="item-left">上传人脸照片</view>
        <view class="item-right">
          <image class="right-pic" :src="model.face_photo" />
          <view class="rightIcon">
            <u-icon
              slot="right"
              name="arrow-right"
              color="#c0c4cc"
              size="16"
            ></u-icon>
          </view>
        </view>
      </u-form-item>
      <view class="sub">
        <button @click="handleSave">保存</button>
      </view>
    </u--form>
    <u-action-sheet
      :show="showSex"
      :actions="actions"
      title="请选择性别"
      description="如果选择保密会报错"
      @close="showSex = false"
      @select="sexSelect"
    >
    </u-action-sheet>
    <div>将本页改成，taro那里面对应的这一页的样式</div>
  </view>
</template>

<script>
import { request, requestData } from '@/utils/http.js'
export default {
  data() {
    return {
      carriedUserid: '',
      model: {
        face_photo: require('@/static/img/my-pic-default.png'),
      },
      rules: {},
    }
  },
  onLaunch: function () {},
  onReady() {
    this.$refs.uForm.setRules(this.rules)
  },
  onLoad(option) {
    if (option && option.id) {
      console.log(option.id)
      this.carriedUserid = option.id
    }
  },
  onShow: function () {
    this.initUesrInfo()
  },
  methods: {
    initUesrInfo() {
      if (this.carriedUserid) {
        //携行用户
        request('/user/carried/info', {
          carried_user_id: this.carriedUserid,
          with_health: 1,
        })
          .then((res) => {
            this.model = {
              ...this.model,
              ...{
                face_photo:
                  res?.datas?.face_photo ||
                  require('@/static/img/my-pic-default.png'),
              },
            }
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        //本人
        request('/user/info/get', {
          with_health: 1,
        })
          .then((res) => {
            this.model = {
              ...this.model,
              ...{
                face_photo:
                  res?.datas?.face_photo ||
                  require('@/static/img/my-pic-default.png'),
              },
            }
          })
          .catch((err) => {
            console.log(err)
          })
      }
    },
    // 表单提交
    handleSave() {
      let user_id = uni.getStorageSync('user_id')
      if (!user_id) {
        uni.showToast({
          title: '请先绑定手机号',
          icon: 'none',
        })
        setTimeout(() => {
          uni.navigateTo({
            url: '/pages/loginPhone/loginPhone',
          })
        }, 1000)
      } else {
        this.$refs.uForm
          .validate()
          .then((res) => {})
          .catch((errors) => {
            console.log(errors)
            uni.$u.toast('请检查填写的内容是否符合规则')
          })
      }
    },
    //uplod fn
    uploadHealth(str) {
      let param = {}
      console.log(this.carriedUserid)
      if (this.carriedUserid) {
        param = {
          user_id: this.carriedUserid,
          image_base64: str,
          image_ext: 'jpeg',
        }
      } else {
        param = {
          user_id: uni.getStorageSync('user_id'),
          image_base64: str,
          image_ext: 'jpeg',
        }
      }
      request('/user/facephoto/base64image/upload', param)
        .then((res) => {
          this.model = {
            ...this.model,
            ...{
              face_photo:
                res?.datas?.face_photo ||
                require('@/static/img/my-pic-default.png'),
            },
          }

          uni.showToast({
            title: '上传成功！',
            icon: 'success',
          })
          this.initUesrInfo()
        })
        .catch((err) => {
          console.log(err)
        })
    },

    //uplod
    handleImg() {
      let that = this
      uni.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          if (res.tempFiles.length > 0) {
            const tempFilePath = res.tempFiles[0].tempFilePath
            uni.getFileSystemManager().readFile({
              filePath: tempFilePath,
              encoding: 'base64',
              success: (data) => {
                let str = data.data
                that.uploadHealth(str) //上传fn
                //this.initUesrInfo()
              },
              fail: (err) => {
                uni.showToast({
                  title: JSON.stringify(err),
                  duration: 200000,
                })
                console.error('读取文件失败:', err)
              },
            })
          }
        },
      })
    },
  },
}
</script>

<style lang="scss" scoped>
/* 更改form样式
	https://gitcode.csdn.net/65ec454d1a836825ed795f5b.html?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTIxMDQ4LCJleHAiOjE3MjQwNTQxMzUsImlhdCI6MTcyMzQ0OTMzNSwidXNlcm5hbWUiOiJxcV8zMzMzMjE4NCJ9.oGTHEemmtk-LQyb8Q8wT6vnYMGpHcw0g2D1P3RXuFXE
	 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.vue-ref {
  width: 100%;
}

.u-form {
  width: 100%;
  height: 100vh;
  padding: 0 20rpx;
  box-sizing: border-box;
}

::v-deep .u-form-item {
  width: 100% !important;
  height: 100% !important;
  box-sizing: border-box !important;
  font-size: 32rpx !important;
  // background: red !important;
}

::v-deep .u-form .u-form-item__body__right {
  flex-direction: row !important;
}

::v-deep .u-form-item__body__right__content__slot {
  flex-direction: row !important;
  width: 100%;
}

::v-deep .u-form-item__body__right_no_flex {
  width: 100% !important;
  height: 100% !important;
}

::v-deep .u-form-item__body {
  padding: 0 !important;
  height: 100% !important;
}

.item {
  width: 100%;
  padding: 0 20rpx;
  box-sizing: border-box;
  height: 100rpx;
  background: #fff;
  border-bottom: 1px solid #f6f6f6;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .item-left {
    width: 30%;
    display: flex;
    align-items: center;
    padding-left: 20rpx;
  }

  .item-right {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}

.right-txt {
}

.rightIcon {
  margin-left: 20rpx;
}

.right-pic {
  width: 60rpx;
  height: 60rpx;
}

.avatarUrl {
  border-radius: 50%;
}

.avatar-wrapper {
  padding: 0;
  width: 60rpx;
  height: 60rpx;
  border-radius: 8rpx;
}

.avatar {
  display: block;
  width: 100%;
  height: 100%;
}

.container {
  display: flex;
}

.u-form-item__body__right__message {
  margin-left: 0;
}

::v-deep .u-button--info {
  background: $global-color-golden !important;
  width: 130rpx !important;
  z-index: 999;
  color: #fff !important;
  // border-radius: 20rpx !important;
}

.phone-box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.sub {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 60rpx;
}

.sub button {
  border: none;
  width: 60%;
  height: 80rpx;
  line-height: 80rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: $global-color-golden;
  border-radius: 40rpx;
  color: #fff;
  font-size: 32rpx;
  font-weight: 400;
}
</style>
